package org.dawnsql.stock.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.List;

@Controller
public class MyDownDataToDbController {

    private Gson gson = new GsonBuilder()
            .enableComplexMapKeySerialization()
            .setDateFormat("yyyy-MM-dd HH:mm:ss")
            .create();

    public void insert_data(String table_name, List<List<Object>> lst, String symbol) throws SQLException, ClassNotFoundException {
        Class.forName("org.gridgain.internal.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:gg-h2:/Users/chenfei/temp/db/" + table_name);

        for (List<Object> r : lst) {
            PreparedStatement statement = conn.prepareStatement("insert into " + table_name + " (st_timestamp, volume, open, high, low, close, chg, percent, turnoverrate, amount, volume_post, amount_post, pe, pb, ps, pcf, market_capital, balance, hold_volume_cn, hold_ratio_cn, net_volume_cn, hold_volume_hk, hold_ratio_hk, net_volume_hk, symbol) " +
                    "values (?, ?, ?, ?, ?, " +
                    "?, ?, ?, ?, ?," +
                    "?, ?, ?, ?, ?," +
                    "?, ?, ?, ?, ?," +
                    "?, ?, ?, ?, ?)");
            statement.setDate(1, new Date((int)r.get(0)));
            statement.setDouble(2, r.get(1) != null? (Double)r.get(1): null);
            statement.setDouble(3, r.get(2) != null? (Double)r.get(2): null);
            statement.setDouble(4, r.get(3) != null? (Double)r.get(3): null);
            statement.setDouble(5, r.get(4) != null? (Double)r.get(4): null);
            statement.setDouble(6, r.get(5) != null? (Double)r.get(5): null);
            statement.setDouble(7, r.get(6) != null? (Double)r.get(6): null);
            statement.setDouble(8, r.get(7) != null? (Double)r.get(7): null);
            statement.setDouble(9, r.get(8) != null? (Double)r.get(8): null);
            statement.setDouble(10, r.get(9) != null? (Double)r.get(9): null);
            statement.setDouble(11, r.get(10) != null? (Double)r.get(10): null);

            statement.setDouble(12, r.get(11) != null? (Double)r.get(11): null);
            statement.setDouble(13, r.get(12) != null? (Double)r.get(12): null);
            statement.setDouble(14, r.get(13) != null? (Double)r.get(13): null);
            statement.setDouble(15, r.get(14) != null? (Double)r.get(14): null);
            statement.setDouble(16, r.get(15) != null? (Double)r.get(15): null);
            statement.setDouble(17, r.get(16) != null? (Double)r.get(16): null);
            statement.setDouble(18, r.get(17) != null? (Double)r.get(17): null);
            statement.setDouble(19, r.get(18) != null? (Double)r.get(18): null);
            statement.setDouble(20, r.get(19) != null? (Double)r.get(19): null);
            statement.setDouble(21, r.get(20) != null? (Double)r.get(20): null);
            statement.setDouble(22, r.get(21) != null? (Double)r.get(21): null);
            statement.setDouble(23, r.get(22) != null? (Double)r.get(22): null);
            statement.setDouble(24, r.get(23) != null? (Double)r.get(23): null);
            statement.setString(25, symbol);

            statement.executeUpdate();
        }
        conn.close();
    }

    @CrossOrigin(origins = "https://xueqiu.com", maxAge = 3600)
    @RequestMapping(value = "/run_sql_data")
    public @ResponseBody
    String run_sql_data(ModelMap model, HttpServletRequest request,
                   HttpServletResponse response,
                        @RequestParam(value = "symbol", required = false) String symbol,
                        @RequestParam(value = "code", required = false) String code,
                        @RequestParam(value = "period", required = false) String period) throws UnsupportedEncodingException, SQLException, ClassNotFoundException {

        String jsonpCallback = request.getParameter("callback");
        if (code != null) {
            List<List<Object>> lst = gson.fromJson(code, new TypeToken<List<List<Object>>>() {}.getType());
            insert_data("stock_date", lst, symbol);
            System.out.println(lst);
            return jsonpCallback + "({data:'true'})";
        }
        return jsonpCallback + "({data:'false'})";
    }
}





































